<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\StoreCreateRequest;
use App\Models\Store;
use App\Models\StoreCategory;
use App\Models\StoreDetail;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class StoreController extends Controller
{
    /**
    * Display a listing of the resource.
    *
    * @return \Illuminate\Http\Response
    */
    public function index()
    {
       return view('admin.store.index');
    }

    public function data(Request $request)
    {
        $model = Store::query();
        if ($request->get('name')){
            $model = $model->where('name','like','%'.$request->get('name').'%');
        }
        if ($request->get('phone')){
            $model = $model->where('phone','like','%'.$request->get('phone').'%');
        }
        $res = $model->orderBy('created_at','desc')->paginate($request->get('limit',30))->toArray();

        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return response()->json($data);
    }

    public function create()
    {
        $categorys = StoreCategory::all();

        return view('admin.store.create',['categorys' => $categorys]);
    }

    public function store(StoreCreateRequest $request)
    {
        $data['category_id'] = $request->input('category_id');
        $data['name'] = $request->input('name');
        $data['phone'] = $request->input('phone');
        $data['area'] = $request->input('area');
        $data['longitude'] = $request->input('longitude');
        $data['latitude'] = $request->input('latitude');
        $data['province_name'] = $request->input('province_name');
        $data['province_code'] = $request->input('province_code');
        $data['city_name'] = $request->input('city_name');
        $data['city_code'] = $request->input('city_code');
        $data['is_close'] = $request->input('is_close');
        $data['logo'] = $request->input('logo');
        $data['open_time'] = $request->input('open_time');
        $data['price'] = $request->input('price');
        $data['cash_proportion'] = $request->input('cash_proportion');
        $data['bonus_pool_proportion'] = $request->input('bonus_pool_proportion');
        $data['created_at'] = date("Y-m-d h:i:s");
        $data['updated_at'] = date("Y-m-d h:i:s");
        $data['is_show'] = $request->input('is_show');
        if ($data['is_close'] == 'on') {
            $data['is_close'] = 1;
        } else {
            $data['is_close'] = 0;
        }
        if ($data['is_show'] == 'on') {
            $data['is_show'] = 1;
        } else {
            $data['is_show'] = 0;
        }

        $data['discount'] = $request->input('discount');


        if ($request->input('background')) {
            $data['background'] = $request->input('background');
            $data['background'] = implode(',', $data['background']);
        }

        $message = $request->input('message');
        $id = Store::insertGetId($data);

        StoreDetail::insert([
            'store_id' => $id,
            'message' => $message
        ]);

        if (1) {
            $return = [
                'code' => 0,
                'msg'   => '成功',
            ];
        } else {
            $return = [
                'code' => -1,
                'msg'   => '失败',
            ];
        }
        return response()->json($return);
    }

    public function edit($id)
    {
        $store = Store::findOrFail($id);
        $categorys = StoreCategory::all();

        $store['background'] = explode(',', $store['background']);

        $store_message = StoreDetail::where('store_id', $id)->first();
        $store['message'] = '';
        if ($store_message) {
            $store['message'] = $store_message->message;
        }

        return view('admin.store.edit',[
            'store' => $store,
            'categorys' => $categorys
        ]);
    }

    public function update(Request $request, $id){
        $store = Store::findOrFail($id);

        $store->category_id = $request->input('category_id');
        $store->name = $request->input('name');
        $store->phone = $request->input('phone');
        $store->area = $request->input('area');
        $store->longitude = $request->input('longitude');
        $store->latitude = $request->input('latitude');
        $store->province_name = $request->input('province_name');
        $store->province_code = $request->input('province_code');
        $store->city_name = $request->input('city_name');
        $store->city_code = $request->input('city_code');
        $store->is_close = $request->input('is_close');
        $store->logo = $request->input('logo');
        $store->price = $request->input('price');
        $store->cash_proportion = $request->input('cash_proportion');
        $store->bonus_pool_proportion = $request->input('bonus_pool_proportion');
        $store->open_time = $request->input('open_time');
        $store->updated_at = date("Y-m-d h:i:s");
        if ($store->is_close == 'on') {
            $store->is_close = 1;
        } else {
            $store->is_close = 0;
        }
        if ($store->is_show == 'on') {
            $store->is_show = 1;
        } else {
            $store->is_show = 0;
        }

        $store->discount = $request->input('discount');


        if ($request->input('background')) {
            $store->background = $request->input('background');
            $store->background = implode(',', $store->background);
        }

        $message = $request->input('message');



        try {
            \DB::beginTransaction();

            $store->save();

            $storeDetail = StoreDetail::where('store_id', $id)->first();
            if ($storeDetail) {
                $storeDetail->message = $message;
                $storeDetail->save();
            }else{
                StoreDetail::insert([
                    'store_id' => $id,
                    'message' => $message
                ]);
            }


            \DB::commit();
            return response()->json([
                'code' => 0,
                'msg'   => '成功',
            ]);
        } catch (\Exception $e) {
            \DB::rollBack();
//            var_dump($e->getMessage());
            return response()->json([
                'code' => -1,
                'msg'   => '失败',
            ]);
        }

    }

}
